package com.test.service.impl;

import com.test.entity.Book;
import com.test.entity.Borrow;
import com.test.entity.User;
import com.test.entity.UserBorrowDetail;
import com.test.mapper.BorrowMapper;
import com.test.service.BorrowService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/*
 **@Author:youpeng
 **Created on 2022/4/15
 */
@Service
public class BorrowServiceImpl implements BorrowService {

    @Resource
    BorrowMapper borrowMapper;

    @Override
    public UserBorrowDetail getBorrowByUid(int uid) {
        List<Borrow> borrowList = borrowMapper.getBorrowByUid(uid);

        RestTemplate template = new RestTemplate();
        User user = template.getForObject("http://localhost:8101/user/"+uid,User.class);

        List<Book> bookList=borrowList
                .stream()
                .map(b->template.getForObject("http://localhost:8201/book/"+b.getBid(),Book.class))
                .collect(Collectors.toList());
        return new UserBorrowDetail(user,bookList);
    }
}
